package tech.aistar.corejava.day10;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:日历类
 * @date 2019/7/26 0026
 */
public class CalendarDemo {
    public static void main(String[] args) {
        //通过简单工厂模式来构建日历对象
        //Calendar - 抽象类 - 永远是不能够被实例化的

        //面向父类编程 - 父类 对象 = new 子类();

         //Calendar cal = new Calendar类的子类();
         Calendar cal = Calendar.getInstance();

         //Calendar c = new GregorianCalendar();

        //System.out.println(new GregorianCalendar().isLeapYear(2012));

        System.out.println(cal);

        //将日历转换成日期
        Date dt = cal.getTime();
        System.out.println(dt);

        //日期转换成日历
        Date at = new Date(System.currentTimeMillis());
        cal.setTime(at);
        System.out.println(cal);


        //重新设置cal
        //根据指定的年月日来构建

        //统一进行赋值
        cal.set(2020,4,1);
        System.out.println(cal.getTime());

        //返回指定日历字段的值
        //int year = cal.get(1);//字面量直接赋值..可读性比较差

        int year = cal.get(Calendar.YEAR);
        System.out.println("year:"+year);

        System.out.println(cal.get(Calendar.MONTH)+1);

        System.out.println(cal.get(Calendar.DAY_OF_MONTH));

        System.out.println(cal.get(Calendar.DAY_OF_WEEK));

        System.out.println(cal.get(Calendar.HOUR));
        System.out.println(cal.get(Calendar.HOUR_OF_DAY));

        System.out.println("======================");
        //可以针对单个字段一一进行赋值
        Calendar dar = Calendar.getInstance();

        //对year进行重新赋值
        //dar.set(1,2022);
        //dar.set(2,4);

        dar.set(Calendar.YEAR,2012);
        dar.set(Calendar.MONTH,4);
        System.out.println(dar.get(Calendar.YEAR));
        System.out.println(dar.get(Calendar.MONTH)+1);
    }
}
